package com.leecode.base;

/**
 * @Author: yaoshubin
 * @Date: 2022-12-11 23:00
 * @description :  双链表反转：比单链表多了个 指针的反转
 *
 */
public class Index_08_双链表反转 {

    public static void main(String[] args) {
        // 用一个单链表表示 1，2，3 , 并按序输出
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);

        Node node = reverseDbNodeList(head);

        while (node != null) {
            System.out.println(node.value);
            node = node.next;
        }
    }


    /**
     * 注意指针顺序不能乱
     * @param head
     * @return
     */
    public static Node reverseDbNodeList(Node head) {
        Node next = null;
        Node pre = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            head.pre = next; //比单链表多这行
            pre = head;
            head = next;
        }
        return pre;
    }
}
